package com.net.xpay.core.manager.impl.auto;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.auto.AutoPayBatchMapper;
import com.net.xpay.core.domain.auto.AutoPayBatch;
import com.net.xpay.core.manager.auto.AutoPayBatchManager;
import com.net.xpay.core.model.AutoPayBatchSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.util.List;


@Component
public class AutoPayBatchManagerImpl extends AbstractVersionedManager<AutoPayBatch> implements AutoPayBatchManager{

    @Autowired
    private AutoPayBatchMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(AutoPayBatch obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        return super.save(obj);
    }

    @Override
    protected BasicMapper<Long, AutoPayBatch> getMapper() {
        return mapper;
    }

    @Override
    public AutoPayBatch findByImportBatchNo(Long userId, String importBatchNo) {
        Preconditions.checkArgument(userId != null && !Strings.isNullOrEmpty(importBatchNo));
        AutoPayBatch condition = new AutoPayBatch();
        condition.setUserId(userId);
        condition.setImportBatchNo(importBatchNo);
        return getByCondition(condition);
    }

    @Override
    public List<AutoPayBatch> search(AutoPayBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(AutoPayBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);

    }

    @Override
    public AutoPayBatch findById( Long userId,Long id) {
        Preconditions.checkArgument(userId != null && id != null);
        AutoPayBatch condition = new AutoPayBatch();
        condition.setUserId(userId);
        condition.setId(id);
        return getByCondition(condition);
    }
}
